package com.fortune.controller;

import com.fortune.base.BaseAPIExcution;
import com.fortune.base.BaseController;
import com.fortune.common.redis.RedisClient;
import com.fortune.constants.PatternConstants;
import com.fortune.constants.redis.CacheKeys;
import com.fortune.dto.excution.SalesManExcution;
import com.fortune.dto.in.ClientRegistered.ClientRegisteredDto;
import com.fortune.dto.in.ClientRegistered.VeriPhoneByDDto;
import com.fortune.dto.in.ClientRegistered.VeriPhoneDto;
import com.fortune.entity.VeriPhoneAndCode;
import com.fortune.enums.SalesManEnum;
import com.fortune.service.COrderLoginService;
import com.fortune.service.ClientRegisteredService;
import com.fortune.util.UUIDUtil;
import com.fortune.vo.COrderLogin.COrderLoginInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.Pattern;


@Api(value = "wechat/clientRegistered", description = "客户账户", tags = {"微信小程序（客户）：账户相关"})
@RestController
@RequestMapping("wechat/clientRegistered")
@Validated
public class ClientRegisteredController extends BaseController{

	@Autowired
	private ClientRegisteredService clientRegisteredService;

	@Autowired
	private RedisClient redisClient;

	@ApiOperation(value = "客户注册", nickname = "registered", notes = "", tags = {"@郭阳"})
	@PostMapping(value = "registered",
			produces = { MediaType.APPLICATION_JSON_VALUE },
			consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> registered(@Validated @RequestBody ClientRegisteredDto clientRegisteredDto) {
		String clientId = UUIDUtil.get32UUID();
		int addClient = clientRegisteredService.addClient(clientRegisteredDto, clientId);
		if(addClient < 1){
			return new SalesManExcution<Void>(null, SalesManEnum.REGISTRATION_ERROR);
		}
		return new SalesManExcution<Void>(null, SalesManEnum.REGISTRATION_SUCCESS);
	}

	@ApiOperation(value = "客户验证", nickname = "veriPhoneAndCode", notes = "", tags = {"@郭阳"})
	@PostMapping(value = "veriPhoneAndCode",
			produces = { MediaType.APPLICATION_JSON_VALUE },
			consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> veriPhoneAndCode(@Validated @RequestBody VeriPhoneAndCode veriPhoneAndCode) {
		String phone = veriPhoneAndCode.getPhone();
		Integer veriPhoneByClient = clientRegisteredService.veriPhoneByClient(phone);
		if(veriPhoneByClient > 0){
			return new SalesManExcution<Void>(null, SalesManEnum.PHONE_EXIST);
		}
		Integer veriPhoneByDealer = clientRegisteredService.veriPhoneByDealer(phone);
		if(veriPhoneByDealer > 0){
			return new SalesManExcution<Void>(null, SalesManEnum.PHONE_EXIST);
		}
		Integer veriPhoneByVendor = clientRegisteredService.veriPhoneByVendor(phone);
		if(veriPhoneByVendor > 0){
			return new SalesManExcution<Void>(null, SalesManEnum.PHONE_EXIST);
		}
		Integer veriPhoneByplatform = clientRegisteredService.veriPhoneByplatform(phone);
		if(veriPhoneByplatform > 0){
			return new SalesManExcution<Void>(null, SalesManEnum.PHONE_EXIST);
		}

		String code = veriPhoneAndCode.getCode();
		String codes = redisClient.get(CacheKeys.EMAIL_SEND_WECHAT_CODE,phone);
		if (!code.equals(codes)) {
			return new SalesManExcution<Void>(null, SalesManEnum.VERIFICATION_ERROR);
		}

		return new SalesManExcution<Void>(null, SalesManEnum.VERIFICATION_SUCCESS);
	}

	@ApiOperation(value = "忘记密码验证手机号", nickname = "veriPhone", notes = "", tags = {"@郭阳"})
	@PostMapping(value = "veriPhone",
			produces = { MediaType.APPLICATION_JSON_VALUE },
			consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> veriPhone(@Validated @RequestBody VeriPhoneAndCode veriPhoneAndCode) {
		String phone = veriPhoneAndCode.getPhone();
		Integer veriPhoneByDealer = clientRegisteredService.veriPhoneByDealer(phone);
		if(veriPhoneByDealer > 0){
			return new SalesManExcution<Void>(null, SalesManEnum.PHONE_INFO_ERROR);
		}
		Integer veriPhoneByVendor = clientRegisteredService.veriPhoneByVendor(phone);
		if(veriPhoneByVendor > 0){
			return new SalesManExcution<Void>(null, SalesManEnum.PHONE_INFO_ERROR);
		}
		Integer veriPhoneByplatform = clientRegisteredService.veriPhoneByplatform(phone);
		if(veriPhoneByplatform > 0){
			return new SalesManExcution<Void>(null, SalesManEnum.PHONE_INFO_ERROR);
		}


		Integer veriPhoneByClient = clientRegisteredService.veriPhoneByClient(phone);
		if(veriPhoneByClient > 0){
			String code = veriPhoneAndCode.getCode();
			String codes = redisClient.get(CacheKeys.EMAIL_SEND_WECHAT_CODE,phone);
			if (!code.equals(codes)) {
				return new SalesManExcution<Void>(null, SalesManEnum.VERIFICATION_ERROR);
			}
			return new SalesManExcution<Void>(null, SalesManEnum.VERIFICATION_SUCCESS);
		}

		return new SalesManExcution<Void>(null, SalesManEnum.PHONE_INFO_ERROR);
	}

	@ApiOperation(value = "验证手机号重复（业务员）", nickname = "addClientVeriPhone", notes = "", tags = {"@郭阳"})
	@PostMapping(value = "addClientVeriPhone",
			produces = { MediaType.APPLICATION_JSON_VALUE },
			consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> addClientVeriPhone(@Validated @RequestBody VeriPhoneByDDto veriPhoneByDDto) {
		String phone = veriPhoneByDDto.getPhone();
		Integer veriPhoneByDealer = clientRegisteredService.veriPhoneByDealer(phone);
		if(veriPhoneByDealer > 0){
			return new SalesManExcution<Void>(null, SalesManEnum.PHONE_INFO_ERROR);
		}
		Integer veriPhoneByVendor = clientRegisteredService.veriPhoneByVendor(phone);
		if(veriPhoneByVendor > 0){
			return new SalesManExcution<Void>(null, SalesManEnum.PHONE_INFO_ERROR);
		}
		Integer veriPhoneByplatform = clientRegisteredService.veriPhoneByplatform(phone);
		if(veriPhoneByplatform > 0){
			return new SalesManExcution<Void>(null, SalesManEnum.PHONE_INFO_ERROR);
		}
		return new SalesManExcution<Void>(null, SalesManEnum.VERIFICATION_SUCCESS);

	}
}
